package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wl156.R;
import com.wl156.model.WebServiceClient;

public class ResetPassword extends Activity implements OnClickListener {

	EditText mMobile;
	EditText mCode;
	EditText mPassword;
	private WebServiceClient mWebServiceClient;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_reset_password);

		mWebServiceClient = new WebServiceClient(this);

		mMobile = (EditText) findViewById(R.id.mobile);
		mCode = (EditText) findViewById(R.id.code);
		mPassword = (EditText) findViewById(R.id.new_password);

		String mobile = getIntent().getStringExtra("mobile");
		mMobile.setText(mobile);

		Button confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Log.d("", "onclick");

		final String mobile = mMobile.getText().toString();
		final String code = mCode.getText().toString();
		final String password = mPassword.getText().toString();

		if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(code)
				|| TextUtils.isEmpty(password)) {
			Toast.makeText(ResetPassword.this, "请输入相应字段", Toast.LENGTH_SHORT)
					.show();
		}

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mProgressDialog;

			protected void onPreExecute() {
				mProgressDialog = ProgressDialog.show(ResetPassword.this, "",
						"请稍后");
			};

			@Override
			protected void onPostExecute(Object result) {
				super.onPostExecute(result);

				mProgressDialog.dismiss();

				if (result instanceof Exception) {
					Exception e = (Exception) result;
					Toast.makeText(ResetPassword.this, "网络异常，请稍后再试",
							Toast.LENGTH_SHORT).show();
				} else if (result instanceof SoapPrimitive) {
					SoapPrimitive sp = (SoapPrimitive) result;
					Toast.makeText(ResetPassword.this, sp.toString(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(ResetPassword.this, "密码设定成功",
							Toast.LENGTH_SHORT).show();
					finish();
				}
			}

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.common_ModifyPWD(code, mobile,
							password);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}
}
